package com.hwtx.form.domain.def;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.geekidea.boot.system.vo.ProValueConfig;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
@JsonInclude(value = JsonInclude.Include.NON_EMPTY)
public class FormConfigExt {
    QueryConfig query;
    FormConfig form;
    ListConfig list;

    public boolean exist() {
        return query != null && !query.empty() || form != null && !form.empty() || list != null && !list.empty();
    }

    @Data
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public static class FormConfigCtr {
        @JsonInclude(JsonInclude.Include.NON_EMPTY)
        Integer ctrType;
        @JsonInclude(JsonInclude.Include.NON_EMPTY)
        private Map<String, Object> comExt;
        @JsonInclude(JsonInclude.Include.NON_EMPTY)
        private Integer width;


        public static Map<String, Object> emptyMap() {
            Map<String, Object> map = new HashMap<>();
            map.put("ctrType", null);
            map.put("ctrSelectType", null);
            return map;
        }

        public boolean empty() {
            return ctrType == null && MapUtils.isEmpty(comExt) && width == null;
        }

        public void clear() {
            ctrType = null;
            width = null;
        }
    }

    @EqualsAndHashCode(callSuper = true, of = {"alias"})
    @Data
    public static class SearchConfig extends ProValueConfig {
        private String alias;
        //查询时使用的字段
        private Map<String, List<String>> addFields;
    }

    @Data
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public static class BaseConfig {
        FormConfigCtr configCtr;

        public boolean empty() {
            return configCtr == null || configCtr.empty();
        }
    }

    @EqualsAndHashCode(callSuper = true)
    @Data
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public static class ListConfig extends BaseConfig {
        SearchConfig search;

        public boolean empty() {
            return search == null && super.empty();
        }
    }

    @EqualsAndHashCode(callSuper = true)
    @Data
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public static class QueryConfig extends ListConfig {
    }

    @EqualsAndHashCode(callSuper = true)
    @Data
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public static class FormConfig extends BaseConfig {
        String labelAlign;
        Integer labelWidth;

        public boolean empty() {
            return super.empty() && StringUtils.isBlank(labelAlign) && labelWidth == null;
        }

        public boolean localEmpty() {
            return StringUtils.isBlank(labelAlign) && labelWidth == null;
        }
    }
}
